import { fileURLToPath, URL } from 'node:url'
import { resolve } from 'node:path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import UnoCSS from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import svgLoader from 'vite-svg-loader'
import SvgComponent from 'unplugin-svg-component/vite'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    svgLoader(),
    // 自动按需导入 API
    AutoImport({
      imports: ['vue', 'vue-router', 'pinia'],
      dts: 'types/auto/auto-imports.d.ts',
      resolvers: [ElementPlusResolver()],
    }),
    // 自动按需导入组件
    Components({
      dts: 'types/auto/components.d.ts',
      resolvers: [ElementPlusResolver()],
    }),
    UnoCSS(),
    // 支持将 SVG 文件导入为 Vue 组件
    svgLoader({
      defaultImport: 'url',
      svgoConfig: {
        plugins: [
          {
            name: 'preset-default',
            params: {
              overrides: {
                // @see https://github.com/svg/svgo/issues/1128
                removeViewBox: false,
              },
            },
          },
        ],
      },
    }),
    // 自动生成 SvgIcon 组件和 SVG 雪碧图
    SvgComponent({
      iconDir: [resolve(__dirname, 'src/common/assets/icons')],
      preserveColor: resolve(
        __dirname,
        'src/common/assets/icons/preserve-color',
      ),
      dts: true,
      dtsDir: resolve(__dirname, 'types/auto'),
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
})
